package com.example.design.utils;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class RedisUtils {


    @Resource
    private RedisTemplate<String,Object> redisTemplate;


    // 获取值
    public Object get(String key){
        if (key == null){
            throw new UnsupportedOperationException("key is null");
        }
        return redisTemplate.opsForValue().get(key);
    }

    // 设置值
    public void set(String key,Object value){
        if (key == null){
            throw new UnsupportedOperationException("key is null");
        }
        redisTemplate.opsForValue().set(key,value);
    }

    // 设置过期时间
    public void set(String key,Object value,long time){
        if (time > 0){
            redisTemplate.opsForValue().set(key,value,time);
        }else {
            set(key,value);
        }
    }


    public void del(String key){
        if (key == null){
            throw new UnsupportedOperationException("key is null");
        }
        redisTemplate.delete(key);
    }
}
